package com.woniu.finance.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniu.finance.entity.Asset;
import com.woniu.finance.entity.Bill;
import com.woniu.finance.entity.RequestParam;
import com.woniu.finance.mapper.AssetMapper;
import com.woniu.finance.service.IAssetService;
import com.woniu.util.utils.HttpResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 资产表 服务实现类
 * </p>
 *
 * @author 刘品水
 * @since 2023-04-25
 */
@Service
public class AssetServiceImpl implements IAssetService {
    @Autowired(required = false)
    private AssetMapper assetMapper;

    @Override
    public HttpResult<?> findAllAsset(RequestParam<Asset> param) {

        //分页开始
        PageHelper.startPage(param.getPageNum(), param.getPageSize());
        //调用mapper层接口
        List<Asset> allAsset = assetMapper.findAllAsset();

        //把mapper接口返回参数放入pageInfo
        PageInfo pageInfo = new PageInfo(allAsset);

        return new HttpResult<>().ok(pageInfo);
    }

    /**
     * @Description: 保存Asset（资产）
     * @Param: [asset]
     * @return: com.woniu.util.utils.HttpResult<?>
     * @Author: 阿水
     * @Date: 2023/4/27
     */
    @Override
    public HttpResult<?> saveAsset(Asset asset) {
        try {
            assetMapper.insert(asset);
            return new HttpResult<>().ok();
        } catch (Exception e) {
            e.printStackTrace();
            return new HttpResult<>().fail();
        }
    }

    @Override
    public HttpResult<?> modifyAsset(Asset asset) {
        try {
            assetMapper.updateById(asset);
            return new HttpResult<>().ok();
        } catch (Exception e) {
            e.printStackTrace();
            return new HttpResult<>().fail();
        }
    }

    @Override
    public HttpResult<?> removeAssetById(Integer id) {
        try {
            assetMapper.deleteById(id);
            return new HttpResult<>().ok();
        } catch (Exception e) {
            e.printStackTrace();
            return new HttpResult<>().fail();
        }
    }

    @Override
    public HttpResult<?> findAssetById(Integer id) {
        try {
            Asset asset = assetMapper.selectById(id);
            return new HttpResult<>().ok(asset);
        } catch (Exception e) {
            e.printStackTrace();
            return new HttpResult<>().fail();
        }
    }

    @Override
    public HttpResult<?> findAllAssetByFinanceId(RequestParam<Asset> param) {
        //分页开始
        PageHelper.startPage(param.getPageNum(), param.getPageSize());
        //调用mapper层接口
        List<Asset> allAsset = assetMapper.findAllAssetByFinanceId(param.getData().getOwnedEnterprise());

        //把mapper接口返回参数放入pageInfo
        PageInfo pageInfo = new PageInfo(allAsset);

        return new HttpResult<>().ok(pageInfo);
    }
}
